package com.anuprayog.treasureloot;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import com.anuprayog.treasureloot.model.Bag;
import com.anuprayog.treasureloot.model.Coin;
import com.anuprayog.treasureloot.model.Bomb;
import com.anuprayog.treasureloot.model.Diamond;
import com.anuprayog.treasureloot.model.Ffwd;
import com.anuprayog.treasureloot.model.Hundred;
import com.anuprayog.treasureloot.model.Slow;
import com.anuprayog.treasureloot.model.Ten;
import com.anuprayog.treasureloot.model.components.Speed;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * @author amit
 * This is the main surface that handles the ontouch events and draws
 * the images to the screen.
 */
public class MainGamePanel extends SurfaceView implements
		SurfaceHolder.Callback {

	public static String HIGH_SCORE = "com.anuprayog.treasureloot.SCORE";
	public static String SOUND_OFF_STRING = "com.anuprayog.treasureloot.SOUND";
	private static final float SPEED_INCREASE_FACTOR = (float) 4/3;
	  
	private MainThread thread;
	private Bag bag;
	public int score=0;
	private int warned=0;
	private String scoreString;
	private ArrayList<Coin> coinList = new ArrayList<Coin>();
	private ArrayList<Coin> coinList1 = new ArrayList<Coin>();
	private ArrayList<Bomb> bombList = new ArrayList<Bomb>();
	private ArrayList<Bomb> bombList1 = new ArrayList<Bomb>();
	private ArrayList<Diamond> diamondList = new ArrayList<Diamond>();
	private ArrayList<Ffwd> ffwdList = new ArrayList<Ffwd>();
	private ArrayList<Slow> slowList = new ArrayList<Slow>();
	private ArrayList<Ten> tenList = new ArrayList<Ten>();
	private ArrayList<Hundred> hundredList = new ArrayList<Hundred>();
	
	private int cycle=0;
	private int xcycle;
	private int drawcycle=0;
	private int speedIncreaser=0;
	private int speedVector;
	public boolean paused;
	private boolean restart;
	
	private Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.cloud2);
	
	private Bitmap scorer = BitmapFactory.decodeResource(getResources(), R.drawable.scorer);
	private Bitmap pause = BitmapFactory.decodeResource(getResources(), R.drawable.pause);
	private Bitmap resume = BitmapFactory.decodeResource(getResources(), R.drawable.resume);
	
	private Bitmap zero = BitmapFactory.decodeResource(getResources(), R.drawable.zero);
	private Bitmap one = BitmapFactory.decodeResource(getResources(), R.drawable.one);
	private Bitmap two = BitmapFactory.decodeResource(getResources(), R.drawable.two);
	private Bitmap three = BitmapFactory.decodeResource(getResources(), R.drawable.three);
	private Bitmap four = BitmapFactory.decodeResource(getResources(), R.drawable.four);
	private Bitmap five = BitmapFactory.decodeResource(getResources(), R.drawable.five);
	private Bitmap six = BitmapFactory.decodeResource(getResources(), R.drawable.six);
	private Bitmap seven = BitmapFactory.decodeResource(getResources(), R.drawable.seven);
	private Bitmap eight = BitmapFactory.decodeResource(getResources(), R.drawable.eight);
	private Bitmap nine = BitmapFactory.decodeResource(getResources(), R.drawable.nine);
	
	private MediaPlayer coinClick = MediaPlayer.create((Activity)getContext(), R.raw.coin);

	Intent in = ((Activity)getContext()).getIntent();
	Paint paint = new Paint();
	
	private String soundoff = in.getStringExtra(MainActivity.SOUND_ON_STRING);
		
	public MainGamePanel(Context context) {
		super(context);
		// adding the callback (this) to the surface holder to intercept events
		getHolder().addCallback(this);

		// create the game loop thread
		thread = new MainThread(getHolder(), this);
		
		// make the GamePanel focusable so it can handle events
		setFocusable(true);
		
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {

		int wd = this.getWidth();
		int ht = this.getHeight();
		
		// create droid and load bitmap
		bag = new Bag(BitmapFactory.decodeResource(getResources(), R.drawable.bag), wd/2, 3*(ht/4));
		
		//if it is the first time the thread starts
		if(thread.getState() == Thread.State.NEW){
		thread.setRunning(true);
		thread.start();
		}
	
		//after a pause it starts the thread again
		else
		if (thread.getState() == Thread.State.TERMINATED){
		thread = new MainThread(getHolder(), this);
		thread.setRunning(true);
		thread.start(); // Start a new thread
		restart = true;
		}

		if (restart) {
			thread.setRunning(false);
			paused = true;
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// tell the thread to shut down and wait for it to finish
		// this is a clean shutdown
		boolean retry = true;
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				// try again shutting down the thread
			}
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			// delegating event handling to the smilies giving score of 1 on punching sad smilies and a warning on punching happy smilies
			
			//pause-resume function
			if (event.getX() < getWidth()/5) {
				if (event.getY() < 2*getHeight()/15) {
					if (!paused){
		                thread.setRunning(false);
		                paused=true;
		            } else if (paused){
		            	thread = new MainThread(getHolder(), this);
		        		thread.setRunning(true);
		        		thread.start();
						paused=false;
					}
				}
			}
			
			// delegating event handling to the bag
            bag.handleActionDown((int)event.getX(), (int)event.getY());
            
        } if (event.getAction() == MotionEvent.ACTION_MOVE) {
			// the gestures
        	if(bag.isTouched()){
	            if (!paused) {
	                // the bag was picked up and is being dragged
	                bag.setX((int)event.getX());
	                bag.setY((int)event.getY());
	            }
        	}
        } if (event.getAction() == MotionEvent.ACTION_UP) {
			// touch was released
	            if (bag.isTouched()) {
	                bag.setTouched(false);
	            }
            if (event.getX() < getWidth()/5) {
				if (event.getY() < 2*getHeight()/15) {
					if (!paused){
		                paused=false;
		            } else if (paused){
		            	paused=true;
					}
				}
			}
			
		}
		return true;
	}
	
	

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		
		drawcycle= drawcycle + 1;
		
		canvas.drawBitmap(background, null, new Rect(0,0,getWidth(), getHeight()), paint);
		
		// fills the canvas with smilies
		bag.draw(canvas);
		try {
			for(int i=0;i<coinList.size();i++)
			{
				if(((int)cycle/10)%2==0){
					coinList.get(i).draw(canvas);
				}else{
					coinList1.get(i).draw(canvas);
				}
				
			}
			for(int i=0;i<bombList.size();i++)
			{
				if(((int)cycle/15)%3==0 || ((int)cycle/15)%3==1){
					bombList.get(i).draw(canvas);
				}else{
					bombList1.get(i).draw(canvas);
				}
			}
			for(int i=0;i<diamondList.size();i++)
			{
				diamondList.get(i).draw(canvas);
			}
			for(int i=0;i<ffwdList.size();i++)
			{
				ffwdList.get(i).draw(canvas);
			}
			for(int i=0;i<slowList.size();i++)
			{
				slowList.get(i).draw(canvas);
			}
			for(int i=0;i<tenList.size();i++)
			{
				tenList.get(i).draw(canvas);
			}
			for(int i=0;i<hundredList.size();i++)
			{
				hundredList.get(i).draw(canvas);
			}
		    
		} catch (IndexOutOfBoundsException e) {
			
		} catch (NullPointerException e) {
			
		}	
		
		
	    paint.setARGB(255, 255, 255, 255);
	    
	    paint.setTextSize(this.getHeight()/20);
	    
	    //draw the pause and resume buttons
	    if(!(MainThread.running)){
	    	canvas.drawBitmap(resume, null, new Rect(0,0,getWidth()/5, 2*(getHeight()/15)), paint);
		} else if(MainThread.running){
			canvas.drawBitmap(pause, null, new Rect(0,0,getWidth()/5, 2*(getHeight()/15)), paint);
		}
	    
	    scoreString = String.valueOf(score);
	    paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        //turn antialiasing on
        paint.setAntiAlias(true);
        
        // show score and warnings
		canvas.drawBitmap(scorer, null, new Rect(this.getWidth()/2, this.getHeight()/30, 7*this.getWidth()/10, this.getHeight()/15), paint);
		
		scoreString = String.valueOf(score);
		
		//parsing score string to print score in the canvas
		if (scoreString.charAt(0) == '0'){
			canvas.drawBitmap(zero, null, new Rect(7*this.getWidth()/10, this.getHeight()/30, 3*this.getWidth()/4, this.getHeight()/15), paint);
		} else if (scoreString.charAt(0) == '1'){
			canvas.drawBitmap(one, null, new Rect(7*this.getWidth()/10, this.getHeight()/30, 3*this.getWidth()/4, this.getHeight()/15), paint);
		} else if (scoreString.charAt(0) == '2'){
			canvas.drawBitmap(two, null, new Rect(7*this.getWidth()/10, this.getHeight()/30, 3*this.getWidth()/4, this.getHeight()/15), paint);
		} else if (scoreString.charAt(0) == '3'){
			canvas.drawBitmap(three, null, new Rect(7*this.getWidth()/10, this.getHeight()/30, 3*this.getWidth()/4, this.getHeight()/15), paint);
		} else if (scoreString.charAt(0) == '4'){
			canvas.drawBitmap(four, null, new Rect(7*this.getWidth()/10, this.getHeight()/30, 3*this.getWidth()/4, this.getHeight()/15), paint);
		} else if (scoreString.charAt(0) == '5'){
			canvas.drawBitmap(five, null, new Rect(7*this.getWidth()/10, this.getHeight()/30, 3*this.getWidth()/4, this.getHeight()/15), paint);
		} else if (scoreString.charAt(0) == '6'){
			canvas.drawBitmap(six, null, new Rect(7*this.getWidth()/10, this.getHeight()/30, 3*this.getWidth()/4, this.getHeight()/15), paint);
		} else if (scoreString.charAt(0) == '7'){
			canvas.drawBitmap(seven, null, new Rect(7*this.getWidth()/10, this.getHeight()/30, 3*this.getWidth()/4, this.getHeight()/15), paint);
		} else if (scoreString.charAt(0) == '8'){
			canvas.drawBitmap(eight, null, new Rect(7*this.getWidth()/10, this.getHeight()/30, 3*this.getWidth()/4, this.getHeight()/15), paint);
		} else if (scoreString.charAt(0) == '9'){
			canvas.drawBitmap(nine, null, new Rect(7*this.getWidth()/10, this.getHeight()/30, 3*this.getWidth()/4, this.getHeight()/15), paint);
		} 
		
		if (score>9){
			if (scoreString.charAt(1) == '0'){
				canvas.drawBitmap(zero, null, new Rect(3*this.getWidth()/4, this.getHeight()/30, 4*this.getWidth()/5, this.getHeight()/15), paint);
			} else if (scoreString.charAt(1) == '1'){
				canvas.drawBitmap(one, null, new Rect(3*this.getWidth()/4, this.getHeight()/30, 4*this.getWidth()/5, this.getHeight()/15), paint);
			} else if (scoreString.charAt(1) == '2'){
				canvas.drawBitmap(two, null, new Rect(3*this.getWidth()/4, this.getHeight()/30, 4*this.getWidth()/5, this.getHeight()/15), paint);
			} else if (scoreString.charAt(1) == '3'){
				canvas.drawBitmap(three, null, new Rect(3*this.getWidth()/4, this.getHeight()/30, 4*this.getWidth()/5, this.getHeight()/15), paint);
			} else if (scoreString.charAt(1) == '4'){
				canvas.drawBitmap(four, null, new Rect(3*this.getWidth()/4, this.getHeight()/30, 4*this.getWidth()/5, this.getHeight()/15), paint);
			} else if (scoreString.charAt(1) == '5'){
				canvas.drawBitmap(five, null, new Rect(3*this.getWidth()/4, this.getHeight()/30, 4*this.getWidth()/5, this.getHeight()/15), paint);
			} else if (scoreString.charAt(1) == '6'){
				canvas.drawBitmap(six, null, new Rect(3*this.getWidth()/4, this.getHeight()/30, 4*this.getWidth()/5, this.getHeight()/15), paint);
			} else if (scoreString.charAt(1) == '7'){
				canvas.drawBitmap(seven, null, new Rect(3*this.getWidth()/4, this.getHeight()/30, 4*this.getWidth()/5, this.getHeight()/15), paint);
			} else if (scoreString.charAt(1) == '8'){
				canvas.drawBitmap(eight, null, new Rect(3*this.getWidth()/4, this.getHeight()/30, 4*this.getWidth()/5, this.getHeight()/15), paint);
			} else if (scoreString.charAt(1) == '9'){
				canvas.drawBitmap(nine, null, new Rect(3*this.getWidth()/4, this.getHeight()/30, 4*this.getWidth()/5, this.getHeight()/15), paint);
			} 
		}
		
		if (score>99){
			if (scoreString.charAt(2) == '0'){
				canvas.drawBitmap(zero, null, new Rect(4*this.getWidth()/5, this.getHeight()/30, 17*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(2) == '1'){
				canvas.drawBitmap(one, null, new Rect(4*this.getWidth()/5, this.getHeight()/30, 17*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(2) == '2'){
				canvas.drawBitmap(two, null, new Rect(4*this.getWidth()/5, this.getHeight()/30, 17*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(2) == '3'){
				canvas.drawBitmap(three, null, new Rect(4*this.getWidth()/5, this.getHeight()/30, 17*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(2) == '4'){
				canvas.drawBitmap(four, null, new Rect(4*this.getWidth()/5, this.getHeight()/30, 17*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(2) == '5'){
				canvas.drawBitmap(five, null, new Rect(4*this.getWidth()/5, this.getHeight()/30, 17*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(2) == '6'){
				canvas.drawBitmap(six, null, new Rect(4*this.getWidth()/5, this.getHeight()/30, 17*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(2) == '7'){
				canvas.drawBitmap(seven, null, new Rect(4*this.getWidth()/5, this.getHeight()/30, 17*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(2) == '8'){
				canvas.drawBitmap(eight, null, new Rect(4*this.getWidth()/5, this.getHeight()/30, 17*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(2) == '9'){
				canvas.drawBitmap(nine, null, new Rect(4*this.getWidth()/5, this.getHeight()/30, 17*this.getWidth()/20, this.getHeight()/15), paint);
			} 
		}
		
		if (score>999){
			if (scoreString.charAt(3) == '0'){
				canvas.drawBitmap(zero, null, new Rect(17*this.getWidth()/20, this.getHeight()/30, 9*this.getWidth()/10, this.getHeight()/15), paint);
			} else if (scoreString.charAt(3) == '1'){
				canvas.drawBitmap(one, null, new Rect(17*this.getWidth()/20, this.getHeight()/30, 9*this.getWidth()/10, this.getHeight()/15), paint);
			} else if (scoreString.charAt(3) == '2'){
				canvas.drawBitmap(two, null, new Rect(17*this.getWidth()/20, this.getHeight()/30, 9*this.getWidth()/10, this.getHeight()/15), paint);
			} else if (scoreString.charAt(3) == '3'){
				canvas.drawBitmap(three, null, new Rect(17*this.getWidth()/20, this.getHeight()/30, 9*this.getWidth()/10, this.getHeight()/15), paint);
			} else if (scoreString.charAt(3) == '4'){
				canvas.drawBitmap(four, null, new Rect(17*this.getWidth()/20, this.getHeight()/30, 9*this.getWidth()/10, this.getHeight()/15), paint);
			} else if (scoreString.charAt(3) == '5'){
				canvas.drawBitmap(five, null, new Rect(17*this.getWidth()/20, this.getHeight()/30, 9*this.getWidth()/10, this.getHeight()/15), paint);
			} else if (scoreString.charAt(3) == '6'){
				canvas.drawBitmap(six, null, new Rect(17*this.getWidth()/20, this.getHeight()/30, 9*this.getWidth()/10, this.getHeight()/15), paint);
			} else if (scoreString.charAt(3) == '7'){
				canvas.drawBitmap(seven, null, new Rect(17*this.getWidth()/20, this.getHeight()/30, 9*this.getWidth()/10, this.getHeight()/15), paint);
			} else if (scoreString.charAt(3) == '8'){
				canvas.drawBitmap(eight, null, new Rect(17*this.getWidth()/20, this.getHeight()/30, 9*this.getWidth()/10, this.getHeight()/15), paint);
			} else if (scoreString.charAt(3) == '9'){
				canvas.drawBitmap(nine, null, new Rect(17*this.getWidth()/20, this.getHeight()/30, 9*this.getWidth()/10, this.getHeight()/15), paint);
			} 
		}
		
		if (score>9999){
			if (scoreString.charAt(4) == '0'){
				canvas.drawBitmap(zero, null, new Rect(9*this.getWidth()/10, this.getHeight()/30, 19*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(4) == '1'){
				canvas.drawBitmap(one, null, new Rect(9*this.getWidth()/10, this.getHeight()/30, 19*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(4) == '2'){
				canvas.drawBitmap(two, null, new Rect(9*this.getWidth()/10, this.getHeight()/30, 19*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(4) == '3'){
				canvas.drawBitmap(three, null, new Rect(9*this.getWidth()/10, this.getHeight()/30, 19*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(4) == '4'){
				canvas.drawBitmap(four, null, new Rect(9*this.getWidth()/10, this.getHeight()/30, 19*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(4) == '5'){
				canvas.drawBitmap(five, null, new Rect(9*this.getWidth()/10, this.getHeight()/30, 19*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(4) == '6'){
				canvas.drawBitmap(six, null, new Rect(9*this.getWidth()/10, this.getHeight()/30, 19*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(4) == '7'){
				canvas.drawBitmap(seven, null, new Rect(9*this.getWidth()/10, this.getHeight()/30, 19*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(4) == '8'){
				canvas.drawBitmap(eight, null, new Rect(9*this.getWidth()/10, this.getHeight()/30, 19*this.getWidth()/20, this.getHeight()/15), paint);
			} else if (scoreString.charAt(4) == '9'){
				canvas.drawBitmap(nine, null, new Rect(9*this.getWidth()/10, this.getHeight()/30, 19*this.getWidth()/20, this.getHeight()/15), paint);
			} 
		}
		
		if (score>99999){
			if (scoreString.charAt(5) == '0'){
				canvas.drawBitmap(zero, null, new Rect(19*this.getWidth()/20, this.getHeight()/30, this.getWidth(), this.getHeight()/15), paint);
			} else if (scoreString.charAt(5) == '1'){
				canvas.drawBitmap(one, null, new Rect(19*this.getWidth()/20, this.getHeight()/30, this.getWidth(), this.getHeight()/15), paint);
			} else if (scoreString.charAt(5) == '2'){
				canvas.drawBitmap(two, null, new Rect(19*this.getWidth()/20, this.getHeight()/30, this.getWidth(), this.getHeight()/15), paint);
			} else if (scoreString.charAt(5) == '3'){
				canvas.drawBitmap(three, null, new Rect(19*this.getWidth()/20, this.getHeight()/30, this.getWidth(), this.getHeight()/15), paint);
			} else if (scoreString.charAt(5) == '4'){
				canvas.drawBitmap(four, null, new Rect(19*this.getWidth()/20, this.getHeight()/30, this.getWidth(), this.getHeight()/15), paint);
			} else if (scoreString.charAt(5) == '5'){
				canvas.drawBitmap(five, null, new Rect(19*this.getWidth()/20, this.getHeight()/30, this.getWidth(), this.getHeight()/15), paint);
			} else if (scoreString.charAt(5) == '6'){
				canvas.drawBitmap(six, null, new Rect(19*this.getWidth()/20, this.getHeight()/30, this.getWidth(), this.getHeight()/15), paint);
			} else if (scoreString.charAt(5) == '7'){
				canvas.drawBitmap(seven, null, new Rect(19*this.getWidth()/20, this.getHeight()/30, this.getWidth(), this.getHeight()/15), paint);
			} else if (scoreString.charAt(5) == '8'){
				canvas.drawBitmap(eight, null, new Rect(19*this.getWidth()/20, this.getHeight()/30, this.getWidth(), this.getHeight()/15), paint);
			} else if (scoreString.charAt(5) == '9'){
				canvas.drawBitmap(nine, null, new Rect(19*this.getWidth()/20, this.getHeight()/30, this.getWidth(), this.getHeight()/15), paint);
			} 
		}
	}
	
	/**
	 * This is the game update method. It iterates through all the objects
	 * and calls their update method if they have one or calls specific
	 * engine's update method.
	 */
	public void update() {
		
		Random ran = new Random();
		int x,x1=0;
		
		int z = getWidth();
				
		int aa = getHeight();
		
		cycle = cycle+1; //count update cycles
		xcycle = xcycle+1;
		
		//Increase speed after 2000 update cycles are completed
		if(xcycle == 1200){
			
			
			if(speedIncreaser<5){
				speedIncreaser = speedIncreaser+1;
			}else{
				speedIncreaser = 5;
			}
			
			
			try {
				for(int i=0;i<coinList.size();i++)
				{
					coinList.get(i).getSpeed().increaseSpeed();
					coinList1.get(i).getSpeed().increaseSpeed();
				}
				for(int i=0;i<bombList.size();i++)
				{
					bombList.get(i).getSpeed().increaseSpeed();
					bombList1.get(i).getSpeed().increaseSpeed();
				}
				for(int i=0;i<diamondList.size();i++)
				{
					diamondList.get(i).getSpeed().increaseSpeed();
				}
				for(int i=0;i<ffwdList.size();i++)
				{
					ffwdList.get(i).getSpeed().increaseSpeed();
				}
				for(int i=0;i<slowList.size();i++)
				{
					slowList.get(i).getSpeed().increaseSpeed();
				}
				for(int i=0;i<tenList.size();i++)
				{
					tenList.get(i).getScoreSpeed().increaseSpeed();
				}
				for(int i=0;i<hundredList.size();i++)
				{
					hundredList.get(i).getScoreSpeed().increaseSpeed();
				}
			} catch (IndexOutOfBoundsException e) {
				
			} catch (NullPointerException e) {
				
			}
			xcycle=0;
			
		}
		
		//generate smilies after 40 update cycles. increase generation if game speed increases
		if(cycle%((int) (15/(speedIncreaser+1)))==0){
		
			//generate random smiley
			Random random = new Random();
			int gen;
			gen = random.nextInt(100);
			
			x = ran.nextInt(5);
			
			if(x==0){
				x1=z/6;
			} else if(x==1){
				x1=z/3;
			} else if(x==2){
				x1=z/2;
			} else if(x==3){
				x1=2*(z/3);
			} else if(x==4){
				x1=5*(z/6);
			}
			
			speedVector = aa/160;
			
			if(speedVector>=4){
				speedVector = 4;
			}
			
			if(gen<68){
	
				Coin coin = new Coin(BitmapFactory.decodeResource(getResources(), R.drawable.coin), x1, 0);
				Coin coin1 = new Coin(BitmapFactory.decodeResource(getResources(), R.drawable.coin1), x1, 0);
				coin.getSpeed().setYv((float) (speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
				coin1.getSpeed().setYv((float) (speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
				coinList.add(coin);
				coinList1.add(coin1);
			
			} else if(gen>=68 && gen<76){
		
				Diamond diamond = new Diamond(BitmapFactory.decodeResource(getResources(), R.drawable.diamond1), x1, 0);
				diamond.getSpeed().setYv((float) (speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
				diamondList.add(diamond);
			
			} else if(gen>=76 && gen<80){
				
				Ffwd ffwd = new Ffwd(BitmapFactory.decodeResource(getResources(), R.drawable.ffwd), x1, 0);
				ffwd.getSpeed().setYv((float) (speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
				ffwdList.add(ffwd);
			
			} else if(gen==80 || gen==81){
				
				Slow slow = new Slow(BitmapFactory.decodeResource(getResources(), R.drawable.slow), x1, 0);
				slow.getSpeed().setYv((float) (speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
				slowList.add(slow);
			
			} else if(gen>=82){
		
				Bomb bomb = new Bomb(BitmapFactory.decodeResource(getResources(), R.drawable.clanbomber), x1, 0);
				Bomb bomb1 = new Bomb(BitmapFactory.decodeResource(getResources(), R.drawable.clanbomber1), x1, 0);
				bomb.getSpeed().setYv((float) (speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
				bomb1.getSpeed().setYv((float) (speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
				bombList.add(bomb);
				bombList1.add(bomb1);
			
			}
		}
		
			
		try{
			
		for(int i=0;i<coinList.size();i++){
			if (coinList.get(i).getSpeed().getyDirection() == Speed.DIRECTION_DOWN
					&& coinList.get(i).getY() - coinList.get(i).getBitmap().getHeight() / 2 >= getHeight()) {
				coinList.get(i).getBitmap().recycle();
				coinList1.get(i).getBitmap().recycle();
				coinList.get(i).setBitmap(null);
				coinList1.get(i).setBitmap(null);
				coinList.remove(i);
				coinList1.remove(i);
			}
		}
		
		for(int i=0;i<bombList.size();i++){
			if (bombList.get(i).getSpeed().getyDirection() == Speed.DIRECTION_DOWN
					&& bombList.get(i).getY() - bombList.get(i).getBitmap().getHeight() / 2 >= getHeight()) {
				bombList.get(i).getBitmap().recycle();
				bombList1.get(i).getBitmap().recycle();
				bombList.get(i).setBitmap(null);
				bombList1.get(i).setBitmap(null);
				bombList.remove(i);
				bombList1.remove(i);
			}
		}
		
		for(int i=0;i<diamondList.size();i++){
			if (diamondList.get(i).getSpeed().getyDirection() == Speed.DIRECTION_DOWN
					&& diamondList.get(i).getY() - diamondList.get(i).getBitmap().getHeight() / 2 >= getHeight()) {
				diamondList.get(i).getBitmap().recycle();
				diamondList.get(i).setBitmap(null);
				diamondList.remove(i);
			}
		}
		
		for(int i=0;i<ffwdList.size();i++){
			if (ffwdList.get(i).getSpeed().getyDirection() == Speed.DIRECTION_DOWN
					&& ffwdList.get(i).getY() - ffwdList.get(i).getBitmap().getHeight() / 2 >= getHeight()) {
				ffwdList.get(i).getBitmap().recycle();
				ffwdList.get(i).setBitmap(null);
				ffwdList.remove(i);
			}
		}
		
		for(int i=0;i<slowList.size();i++){
			if (slowList.get(i).getSpeed().getyDirection() == Speed.DIRECTION_DOWN
					&& slowList.get(i).getY() - slowList.get(i).getBitmap().getHeight() / 2 >= getHeight()) {
				slowList.get(i).getBitmap().recycle();
				slowList.get(i).setBitmap(null);
				slowList.remove(i);
			}
		}
			
		for(int i=0;i<tenList.size();i++){
			if (tenList.get(i).getScoreSpeed().getyDirection() == Speed.DIRECTION_UP
					&& tenList.get(i).getY() + tenList.get(i).getBitmap().getHeight() / 2 >= getHeight()) {
				tenList.get(i).getBitmap().recycle();
				tenList.get(i).setBitmap(null);
				tenList.remove(i);
			}
		}
				
		for(int i=0;i<hundredList.size();i++){
			if (hundredList.get(i).getScoreSpeed().getyDirection() == Speed.DIRECTION_UP
					&& hundredList.get(i).getY() + hundredList.get(i).getBitmap().getHeight() / 2 >= getHeight()) {
				hundredList.get(i).getBitmap().recycle();
				hundredList.get(i).setBitmap(null);
				hundredList.remove(i);
			}
		}
		
		} catch (IndexOutOfBoundsException e) {
			
		} catch (NullPointerException e) {
			
		}
		
		// Update the smilies
		
		try {
			Iterator<Coin> iter = coinList.iterator();
			while(iter.hasNext()) {
				iter.next().update();
			}
			Iterator<Coin> iter1 = coinList1.iterator();
			while(iter1.hasNext()) {
				iter1.next().update();
			}
			Iterator<Bomb> iter2 = bombList.iterator();
			while(iter2.hasNext()) {
				iter2.next().update();
			}
			Iterator<Bomb> iter3 = bombList1.iterator();
			while(iter3.hasNext()) {
				iter3.next().update();
			}
			Iterator<Diamond> iter4 = diamondList.iterator();
			while(iter4.hasNext()) {
				iter4.next().update();
			}
			Iterator<Ffwd> iter5 = ffwdList.iterator();
			while(iter5.hasNext()) {
				iter5.next().update();
			}
			Iterator<Slow> iter6 = slowList.iterator();
			while(iter6.hasNext()) {
				iter6.next().update();
			}
			Iterator<Ten> iter7 = tenList.iterator();
			while(iter7.hasNext()) {
				iter7.next().update();
			}
			Iterator<Hundred> iter8 = hundredList.iterator();
			while(iter8.hasNext()) {
				iter8.next().update();
			}
			
		} catch (IndexOutOfBoundsException e) {
			
		} catch (NullPointerException e) {
		
		}
				
	}
	
	
	/**
	 * Detect collisions during the game play
	 */
	public void collisions(){ 
	//bag-coin collisions
		for(int i=0;i<coinList.size();i++)
		{
			if (coinList.get(i).getX() - coinList.get(i).getBitmap().getWidth() / 2 < bag.getX() + bag.getBitmap().getWidth() / 2 && coinList.get(i).getX() + coinList.get(i).getBitmap().getWidth() / 2 > bag.getX() - bag.getBitmap().getWidth() / 2){
				if (coinList.get(i).getY() - coinList.get(i).getBitmap().getHeight() / 2 < bag.getY() + bag.getBitmap().getHeight() / 2 && coinList.get(i).getY() + coinList.get(i).getBitmap().getHeight() / 2 > bag.getY() - bag.getBitmap().getHeight() / 2){
					if (soundoff.equals("false")){
						coinClick.start();
						}
					
						if(speedIncreaser==0){
						score=score+10;
						Ten ten = new Ten(BitmapFactory.decodeResource(getResources(), R.drawable.ten), coinList.get(i).getX() + coinList.get(i).getBitmap().getWidth()/2, coinList.get(i).getY());
						ten.getScoreSpeed().setYv((float) (-2*speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
						tenList.add(ten);
						}else if(speedIncreaser==1){
						score=score+12;
						Ten ten = new Ten(BitmapFactory.decodeResource(getResources(), R.drawable.twelve), coinList.get(i).getX() + coinList.get(i).getBitmap().getWidth()/2, coinList.get(i).getY());
						ten.getScoreSpeed().setYv((float) (-2*speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
						tenList.add(ten);
						}else if(speedIncreaser==2){
						score=score+14;
						Ten ten = new Ten(BitmapFactory.decodeResource(getResources(), R.drawable.fourteen), coinList.get(i).getX() + coinList.get(i).getBitmap().getWidth()/2, coinList.get(i).getY());
						ten.getScoreSpeed().setYv((float) (-2*speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
						tenList.add(ten);
						}else if(speedIncreaser==3){
						score=score+16;
						Ten ten = new Ten(BitmapFactory.decodeResource(getResources(), R.drawable.sixteen), coinList.get(i).getX() + coinList.get(i).getBitmap().getWidth()/2, coinList.get(i).getY());
						ten.getScoreSpeed().setYv((float) (-2*speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
						tenList.add(ten);
						}else if(speedIncreaser==4){
						score=score+18;
						Ten ten = new Ten(BitmapFactory.decodeResource(getResources(), R.drawable.eighteen), coinList.get(i).getX() + coinList.get(i).getBitmap().getWidth()/2, coinList.get(i).getY());
						ten.getScoreSpeed().setYv((float) (-2*speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
						tenList.add(ten);
						}else if(speedIncreaser==5){
						score=score+20;
						Ten ten = new Ten(BitmapFactory.decodeResource(getResources(), R.drawable.twenty), coinList.get(i).getX() + coinList.get(i).getBitmap().getWidth()/2, coinList.get(i).getY());
						ten.getScoreSpeed().setYv((float) (-2*speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
						tenList.add(ten);
						}
						
					coinList.get(i).getBitmap().recycle();
					coinList1.get(i).getBitmap().recycle();
					coinList.get(i).setBitmap(null);
					coinList1.get(i).setBitmap(null);
					coinList.remove(i);
					coinList1.remove(i);
					
					if (score%10000 < speedIncreaser*2 + 10){
						
						for(i=0;i<coinList.size();i++){
							coinList.get(i).getBitmap().recycle();
							coinList.get(i).setBitmap(null);
							coinList.remove(i);
						}
						for(i=0;i<coinList1.size();i++){
							coinList1.get(i).getBitmap().recycle();
							coinList1.get(i).setBitmap(null);
							coinList1.remove(i);
						}
						for(i=0;i<bombList.size();i++){
							bombList.get(i).getBitmap().recycle();
							bombList.get(i).setBitmap(null);
							bombList.remove(i);
						}
						for(i=0;i<bombList1.size();i++){
							bombList1.get(i).getBitmap().recycle();
							bombList1.get(i).setBitmap(null);
							bombList1.remove(i);
						}
						for(i=0;i<diamondList.size();i++){
							diamondList.get(i).getBitmap().recycle();
							diamondList.get(i).setBitmap(null);
							diamondList.remove(i);
						}
						for(i=0;i<ffwdList.size();i++){
							ffwdList.get(i).getBitmap().recycle();
							ffwdList.get(i).setBitmap(null);
							ffwdList.remove(i);
						}
						for(i=0;i<slowList.size();i++){
							slowList.get(i).getBitmap().recycle();
							slowList.get(i).setBitmap(null);
							slowList.remove(i);
						}
						for(i=0;i<tenList.size();i++){
							tenList.get(i).getBitmap().recycle();
							tenList.get(i).setBitmap(null);
							tenList.remove(i);
						}
						for(i=0;i<hundredList.size();i++){
							hundredList.get(i).getBitmap().recycle();
							hundredList.get(i).setBitmap(null);
							hundredList.remove(i);
						}
						
						coinList.clear();
						coinList1.clear();
						bombList.clear();
						bombList1.clear();
						diamondList.clear();
						ffwdList.clear();
						slowList.clear();
						tenList.clear();
						hundredList.clear();
						
						//thread.setRunning(false);
						//paused = true;
						
						System.gc();
						
					}
				}
			}
			
		}
		
		//bag-bomb collisions
		for(int i=0;i<bombList.size();i++)
		{
			if (bombList.get(i).getX() - bombList.get(i).getBitmap().getWidth() / 2 < bag.getX() + bag.getBitmap().getWidth() / 2 && bombList.get(i).getX() + bombList.get(i).getBitmap().getWidth() / 2 > bag.getX() - bag.getBitmap().getWidth() / 2){
				if (bombList.get(i).getY() - bombList.get(i).getBitmap().getHeight() / 2 < bag.getY() + bag.getBitmap().getHeight() / 2 && bombList.get(i).getY() + bombList.get(i).getBitmap().getHeight() / 2 > bag.getY() - bag.getBitmap().getHeight() / 2){
					warned=warned+1;
					bombList.get(i).getBitmap().recycle();
					bombList1.get(i).getBitmap().recycle();
					bombList.get(i).setBitmap(null);
					bombList1.get(i).setBitmap(null);
					bombList.remove(i);
					bombList1.remove(i);
				}
			}
			
		}
		
		//bag-diamond collisions
		for(int i=0;i<diamondList.size();i++)
		{
			if (diamondList.get(i).getX() - diamondList.get(i).getBitmap().getWidth() / 2 < bag.getX() + bag.getBitmap().getWidth() / 2 && diamondList.get(i).getX() + diamondList.get(i).getBitmap().getWidth() / 2 > bag.getX() - bag.getBitmap().getWidth() / 2){
				if (diamondList.get(i).getY() - diamondList.get(i).getBitmap().getHeight() / 2 < bag.getY() + bag.getBitmap().getHeight() / 2 && diamondList.get(i).getY() + diamondList.get(i).getBitmap().getHeight() / 2 > bag.getY() - bag.getBitmap().getHeight() / 2){
					if (soundoff.equals("false")){
						coinClick.start();
						}
					
						if(speedIncreaser==0){
						score=score+100;
						Hundred hundred = new Hundred(BitmapFactory.decodeResource(getResources(), R.drawable.hundred), diamondList.get(i).getX() + diamondList.get(i).getBitmap().getWidth()/2, diamondList.get(i).getY());
						hundred.getScoreSpeed().setYv((float) (-2*speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
						hundredList.add(hundred);
						}else if(speedIncreaser==1){
						score=score+120;
						Hundred hundred = new Hundred(BitmapFactory.decodeResource(getResources(), R.drawable.hundredtwenty), diamondList.get(i).getX() + diamondList.get(i).getBitmap().getWidth()/2, diamondList.get(i).getY());
						hundred.getScoreSpeed().setYv((float) (-2*speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
						hundredList.add(hundred);
						}else if(speedIncreaser==2){
						score=score+140;
						Hundred hundred = new Hundred(BitmapFactory.decodeResource(getResources(), R.drawable.hundredforty), diamondList.get(i).getX() + diamondList.get(i).getBitmap().getWidth()/2, diamondList.get(i).getY());
						hundred.getScoreSpeed().setYv((float) (-2*speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
						hundredList.add(hundred);
						}else if(speedIncreaser==3){
						score=score+160;
						Hundred hundred = new Hundred(BitmapFactory.decodeResource(getResources(), R.drawable.hundredsixty), diamondList.get(i).getX() + diamondList.get(i).getBitmap().getWidth()/2, diamondList.get(i).getY());
						hundred.getScoreSpeed().setYv((float) (-2*speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
						hundredList.add(hundred);
						}else if(speedIncreaser==4){
						score=score+180;
						Hundred hundred = new Hundred(BitmapFactory.decodeResource(getResources(), R.drawable.hundredeighty), diamondList.get(i).getX() + diamondList.get(i).getBitmap().getWidth()/2, diamondList.get(i).getY());
						hundred.getScoreSpeed().setYv((float) (-2*speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
						hundredList.add(hundred);
						}else if(speedIncreaser==5){
						score=score+200;
						Hundred hundred = new Hundred(BitmapFactory.decodeResource(getResources(), R.drawable.twohundred), diamondList.get(i).getX() + diamondList.get(i).getBitmap().getWidth()/2, diamondList.get(i).getY());
						hundred.getScoreSpeed().setYv((float) (-2*speedVector * Math.pow(SPEED_INCREASE_FACTOR, speedIncreaser)));
						hundredList.add(hundred);
						}
						
					diamondList.get(i).getBitmap().recycle();
					diamondList.get(i).setBitmap(null);
					diamondList.remove(i);
					
					if (score%10000 < speedIncreaser*20 + 100){
						
						for(i=0;i<coinList.size();i++){
							coinList.get(i).getBitmap().recycle();
							coinList.get(i).setBitmap(null);
							coinList.remove(i);
						}
						for(i=0;i<coinList1.size();i++){
							coinList1.get(i).getBitmap().recycle();
							coinList1.get(i).setBitmap(null);
							coinList1.remove(i);
						}
						for(i=0;i<bombList.size();i++){
							bombList.get(i).getBitmap().recycle();
							bombList.get(i).setBitmap(null);
							bombList.remove(i);
						}
						for(i=0;i<bombList1.size();i++){
							bombList1.get(i).getBitmap().recycle();
							bombList1.get(i).setBitmap(null);
							bombList1.remove(i);
						}
						for(i=0;i<diamondList.size();i++){
							diamondList.get(i).getBitmap().recycle();
							diamondList.get(i).setBitmap(null);
							diamondList.remove(i);
						}
						for(i=0;i<ffwdList.size();i++){
							ffwdList.get(i).getBitmap().recycle();
							ffwdList.get(i).setBitmap(null);
							ffwdList.remove(i);
						}
						for(i=0;i<slowList.size();i++){
							slowList.get(i).getBitmap().recycle();
							slowList.get(i).setBitmap(null);
							slowList.remove(i);
						}
						for(i=0;i<tenList.size();i++){
							tenList.get(i).getBitmap().recycle();
							tenList.get(i).setBitmap(null);
							tenList.remove(i);
						}
						for(i=0;i<hundredList.size();i++){
							hundredList.get(i).getBitmap().recycle();
							hundredList.get(i).setBitmap(null);
							hundredList.remove(i);
						}
						
						coinList.clear();
						coinList1.clear();
						bombList.clear();
						bombList1.clear();
						diamondList.clear();
						ffwdList.clear();
						slowList.clear();
						tenList.clear();
						hundredList.clear();
						
						//thread.setRunning(false);
						//paused = true;
						
						System.gc();
						
					}
				}
			}
			
		}
		
		//bag-ffwd collisions
		for(int i=0;i<ffwdList.size();i++)
		{
			if (ffwdList.get(i).getX() - ffwdList.get(i).getBitmap().getWidth() / 2 < bag.getX() + bag.getBitmap().getWidth() / 2 && ffwdList.get(i).getX() + ffwdList.get(i).getBitmap().getWidth() / 2 > bag.getX() - bag.getBitmap().getWidth() / 2){
				if (ffwdList.get(i).getY() - ffwdList.get(i).getBitmap().getHeight() / 2 < bag.getY() + bag.getBitmap().getHeight() / 2 && ffwdList.get(i).getY() + ffwdList.get(i).getBitmap().getHeight() / 2 > bag.getY() - bag.getBitmap().getHeight() / 2){
					if(speedIncreaser<5){
						speedIncreaser = speedIncreaser+1;
						xcycle=0;
						
						try {
							for(int j=0;j<coinList.size();j++)
							{
								coinList.get(j).getSpeed().increaseSpeed();
								coinList1.get(j).getSpeed().increaseSpeed();
							}
							for(int j=0;j<bombList.size();j++)
							{
								bombList.get(j).getSpeed().increaseSpeed();
								bombList1.get(j).getSpeed().increaseSpeed();
							}
							for(int j=0;j<diamondList.size();j++)
							{
								diamondList.get(j).getSpeed().increaseSpeed();
							}
							for(int j=0;j<ffwdList.size();j++)
							{
								ffwdList.get(j).getSpeed().increaseSpeed();
							}
							for(int j=0;j<slowList.size();j++)
							{
								slowList.get(j).getSpeed().increaseSpeed();
							}
							for(int j=0;j<tenList.size();j++)
							{
								tenList.get(j).getScoreSpeed().increaseSpeed();
							}
							for(int j=0;j<hundredList.size();j++)
							{
								hundredList.get(j).getScoreSpeed().increaseSpeed();
							}
						} catch (IndexOutOfBoundsException e) {
							
						} catch (NullPointerException e) {
							
						}
					
					}else{
						speedIncreaser=5;
						xcycle=0;
					}
					
					ffwdList.get(i).getBitmap().recycle();
					ffwdList.get(i).setBitmap(null);
					ffwdList.remove(i);
				}
			}
			
		}
		
		//bag-slow collisions
		for(int i=0;i<slowList.size();i++)
		{
			if (slowList.get(i).getX() - slowList.get(i).getBitmap().getWidth() / 2 < bag.getX() + bag.getBitmap().getWidth() / 2 && slowList.get(i).getX() + slowList.get(i).getBitmap().getWidth() / 2 > bag.getX() - bag.getBitmap().getWidth() / 2){
				if (slowList.get(i).getY() - slowList.get(i).getBitmap().getHeight() / 2 < bag.getY() + bag.getBitmap().getHeight() / 2 && slowList.get(i).getY() + slowList.get(i).getBitmap().getHeight() / 2 > bag.getY() - bag.getBitmap().getHeight() / 2){
					if(speedIncreaser>0){
						speedIncreaser = speedIncreaser-1;
						xcycle=0;
						
						try {
							for(int j=0;j<coinList.size();j++)
							{
								coinList.get(j).getSpeed().decreaseSpeed();
								coinList1.get(j).getSpeed().decreaseSpeed();
							}
							for(int j=0;j<bombList.size();j++)
							{
								bombList.get(j).getSpeed().decreaseSpeed();
								bombList1.get(j).getSpeed().decreaseSpeed();
							}
							for(int j=0;j<diamondList.size();j++)
							{
								diamondList.get(j).getSpeed().decreaseSpeed();
							}
							for(int j=0;j<ffwdList.size();j++)
							{
								ffwdList.get(j).getSpeed().decreaseSpeed();
							}
							for(int j=0;j<slowList.size();j++)
							{
								slowList.get(j).getSpeed().decreaseSpeed();
							}
							for(int j=0;j<tenList.size();j++)
							{
								tenList.get(j).getScoreSpeed().decreaseSpeed();
							}
							for(int j=0;j<hundredList.size();j++)
							{
								hundredList.get(j).getScoreSpeed().decreaseSpeed();
							}
						} catch (IndexOutOfBoundsException e) {
							
						} catch (NullPointerException e) {
							
						}
						
					}else{
						speedIncreaser=0;
						xcycle=0;
					}
					
					slowList.get(i).getBitmap().recycle();
					slowList.get(i).setBitmap(null);
					slowList.remove(i);
				}
			}
			
		}
		
		if(warned>=1){
			thread.setRunning(false);
			
			for(int i=0;i<coinList.size();i++){
				coinList.get(i).getBitmap().recycle();
				coinList.get(i).setBitmap(null);
				coinList.remove(i);
			}
			for(int i=0;i<coinList1.size();i++){
				coinList1.get(i).getBitmap().recycle();
				coinList1.get(i).setBitmap(null);
				coinList1.remove(i);
			}
			for(int i=0;i<bombList.size();i++){
				bombList.get(i).getBitmap().recycle();
				bombList.get(i).setBitmap(null);
				bombList.remove(i);
			}
			for(int i=0;i<bombList1.size();i++){
				bombList1.get(i).getBitmap().recycle();
				bombList1.get(i).setBitmap(null);
				bombList1.remove(i);
			}
			for(int i=0;i<diamondList.size();i++){
				diamondList.get(i).getBitmap().recycle();
				diamondList.get(i).setBitmap(null);
				diamondList.remove(i);
			}
			for(int i=0;i<ffwdList.size();i++){
				ffwdList.get(i).getBitmap().recycle();
				ffwdList.get(i).setBitmap(null);
				ffwdList.remove(i);
			}
			for(int i=0;i<slowList.size();i++){
				slowList.get(i).getBitmap().recycle();
				slowList.get(i).setBitmap(null);
				slowList.remove(i);
			}
			for(int i=0;i<tenList.size();i++){
				tenList.get(i).getBitmap().recycle();
				tenList.get(i).setBitmap(null);
				tenList.remove(i);
			}
			for(int i=0;i<hundredList.size();i++){
				hundredList.get(i).getBitmap().recycle();
				hundredList.get(i).setBitmap(null);
				hundredList.remove(i);
			}
			
			coinList.clear();
			coinList1.clear();
			bombList.clear();
			bombList1.clear();
			diamondList.clear();
			ffwdList.clear();
			slowList.clear();
			tenList.clear();
			hundredList.clear();
			
			//thread.setRunning(false);
			//paused = true;
			
			System.gc();
			
			Intent intent = new Intent((Activity)getContext(), ExplosionActivity.class);
			intent.putExtra(HIGH_SCORE, scoreString);
			intent.putExtra(SOUND_OFF_STRING, soundoff);
			((Activity)getContext()).startActivity(intent);
			((Activity)getContext()).finish();
		}
				
	}

}

